package br.edu.ufcg.embedded.eolica.persistence;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class Preferences {

	private String mRecordStoreName = "Eolica Record Store";
	private RecordStore rs;
	private String userName;
	private String password;
	private String currentRevision;
	private String[] stations;

	public boolean load() {
		try {
			rs = RecordStore.openRecordStore(mRecordStoreName, true);
			System.out.println("LOAD DENTRO");
			if (rs.getNumRecords() > 0) {
				System.out.println("LOAD DENTRO 2");
				userName = new String(rs.getRecord(1));
				System.out.println(new String(rs.getRecord(1)));
				System.out.println(new String(rs.getRecord(2)));
				System.out.println(new String(rs.getRecord(3)));
				password = new String(rs.getRecord(2));
				currentRevision = new String(rs.getRecord(3));

				int length = rs.getNumRecords();
				stations = new String[length - 3];
				for (int i = 3; i < length; i++) {
					stations[i - 3] = new String(rs.getRecord(i+1));
				}
				System.out.println("LOAD DENTRO 3");
				rs.closeRecordStore();
				return true;
			}
			System.out.println("LOAD DENTRO 4");
			rs.closeRecordStore();
			return false;			
			
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public void save() {
		try {			
			RecordStore.deleteRecordStore(mRecordStoreName);
			rs = RecordStore.openRecordStore(mRecordStoreName, true);
			System.out.println("SAVE DENTRO");			
			
//			int deleteLength = rs.getNumRecords();
//			for (int i = 1; i < deleteLength; i++) {
//				rs.deleteRecord(i);		
//				System.out.println("delteadro "+i);
//			}
//			rs.closeRecordStore();
//			rs = RecordStore.openRecordStore(mRecordStoreName, true);
			System.out.println("SAVE DENTRO 2");
			System.out.println("user: "+rs.addRecord(userName.getBytes(), 0, userName.getBytes().length));
			System.out.println("pass: "+rs.addRecord(password.getBytes(), 0, password.getBytes().length));
			if (currentRevision != null)
				rs.addRecord(currentRevision.getBytes(), 0, currentRevision.getBytes().length);
			else rs.addRecord("0".getBytes(), 0, 1);
			System.out.println("SAVE DENTRO 3");
			if (stations != null)
			for (int i = 0; i < stations.length; i++) {
				rs.addRecord(stations[i].getBytes(), 0, stations[i].getBytes().length);				
			}
			System.out.println("SAVE DENTRO 4");
			rs.closeRecordStore();
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public String getmRecordStoreName() {
		return mRecordStoreName;
	}

	public void setmRecordStoreName(String mRecordStoreName) {
		this.mRecordStoreName = mRecordStoreName;
	}

	public RecordStore getRs() {
		return rs;
	}

	public void setRs(RecordStore rs) {
		this.rs = rs;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getCurrentRevision() {
		return currentRevision;
	}

	public void setCurrentRevision(String currentRevision) {
		this.currentRevision = currentRevision;
	}

	public String[] getStations() {
		return stations;
	}

	public void setStations(String[] stations) {
		this.stations = stations;
	}
	

}
